let app = getApp();

Page({
  data: {
    nums:0,
    goodlist: [],
    goodnum:0,
    hours: '0' + 0,   // 时
    minute: '0' + 0,   // 分
    second: '0' + 0    // 秒
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var table_id = e.table_id;
    if(!table_id){
      app.showToast("桌台ID不存在");
      wx.navigateBack();
    }else{
      this.setData({
        table_id:table_id
      })
      this.getTableCartFood();
    }
  },
  // 计时器
  setInterval: function () {
    const that = this
    var second = that.data.second
    var minute = that.data.minute
    var hours = that.data.hours
    setInterval(function () {  // 设置定时器
      second++
      if (second >= 60) {
        second = 0  //  大于等于60秒归零
        minute++
        if (minute >= 60) {
          minute = 0  //  大于等于60分归零
          hours++
          if (hours < 10) {
            // 少于10补零
            that.setData({
              hours: '0' + hours
            })
          } else {
            that.setData({
              hours: hours
            })
          }
        }
        if (minute < 10) {
          // 少于10补零
          that.setData({
            minute: '0' + minute
          })
        } else {
          that.setData({
            minute: minute
          })
        }
      }
      if (second < 10) {
        // 少于10补零
        that.setData({
          second: '0' + second
        })
      } else {
        that.setData({
          second: second
        })
      }
    }, 1000)
  },
  getTableCartFood:function(){
    var t = this;
    app.visit("account/getTableCartFood", {
      table_id:t.data.table_id
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        t.setData({
          goodlist:c.data.list,
          nums:c.data.table.yc_num,
          goodnum:c.data.num,
          hours: c.data.hours,   // 时
          minute: c.data.minute,   // 分
          second: c.data.second    // 秒
        })
        t.setInterval()
      }else{
        app.showToast(c.msg)
      }
    })
  },

  // 增加数量
  addCount(e) {
    var t = this;
    const index = e.currentTarget.dataset.index;
    const id = e.currentTarget.dataset.id;
    let goodlist = this.data.goodlist;
    let num = goodlist[index].num;
    num = num + 1;
    goodlist[index].num = num;
    this.setData({
      goodlist: goodlist
    });
    app.visit("account/updateCartFoodNum", {
      num:num,
      cart_id:id
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        t.getTableCartFood();
      }else{
        app.showToast(c.msg)
      }
    })
  },
  // 减少数量
  minusCount(e) {
    var t = this;
    const index = e.currentTarget.dataset.index;
    const id = e.currentTarget.dataset.id;
    let goodlist = this.data.goodlist;
    let num = goodlist[index].num;
    if (num <= 1) {
      return false;
    }
    num = num - 1;
    goodlist[index].num = num;
    this.setData({
      goodlist: goodlist
    });
    app.visit("account/updateCartFoodNum", {
      num:num,
      cart_id:id
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        t.getTableCartFood();
      }else{
        app.showToast(c.msg)
      }
    })
  },
  deleteList(e) {
    var t = this;
    const index = e.currentTarget.dataset.index;
    const id = e.currentTarget.dataset.id;
    let goodlist = this.data.goodlist;
    goodlist.splice(index, 1);             
    this.setData({
      goodlist: goodlist
    });
    app.visit("account/delCartFood", {
      cart_id:id
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        t.getTableCartFood();
      }else{
        app.showToast(c.msg)
      }
    })
  },

   // 减少数量
  bindMinus: function () {
    var t = this;
    var nums = this.data.nums;
    if (nums > 1) {
      nums--;
      app.visit("account/updateTableJiuCanNum", {
        yc_num:nums,
        table_id:t.data.table_id
      }, "", function(c) {
        console.log(c)
        if(0 == c.code){
          t.getTableCartFood();
        }else{
          app.showToast(c.msg)
        }
      })
    }
    var minusStatus = nums <= 1 ? 'disabled' : 'normal'; 
    this.setData({
      nums: nums,
      minusStatus: minusStatus
    });
  },
  // 增加数量
  bindPlus: function () {
    var nums = this.data.nums;
    nums++;
    var t = this;
    app.visit("account/updateTableJiuCanNum", {
      yc_num:nums,
      table_id:t.data.table_id
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        t.getTableCartFood();
      }else{
        app.showToast(c.msg)
      }
    })
    var minusStatus = nums < 1 ? 'disabled' : 'normal';
    this.setData({
      nums: nums,
      minusStatus: minusStatus
    });
  },

  qingtai:function(){
    var t = this;
    wx.showModal({
      title: '提示',
      content: '确认清台吗',
      success: function (res) {
        if (res.confirm) {
    app.visit("account/cloceQingTai", {
      store_id:wx.getStorageSync("store_id"),
      table_id:t.data.table_id
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        wx.showToast({
          title: '清台成功',
          icon: 'success',
          duration: 5000,
          success:function () {
            setTimeout(function () {
              wx.navigateBack();
            }, 3000);// 分钟转换毫秒
          }
        })
      }else{
        app.showToast(c.msg)
      }
    })
        } else {//这里是点击了取消以后
            console.log('用户点击取消')
        }
      }
    })
  }

})